package cn.lyjuan.android.demofive.ui.slide.scroll;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.*;
import android.widget.*;
import cn.lyjuan.android.demofive.R;


/**
 * Created by chad on 2016/4/17.
 */
public class FrameLayoutActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        MainFrame main = new MainFrame(this);
        MenuFrame menu = new MenuFrame(this);
        ScrollContainer container = new ScrollContainer(this);

        container.addView(menu, lp);
        container.addView(main, lp);

        this.addContentView(container, lp);
    }
}

class ScrollContainer extends RelativeLayout
{
    private Scroller scroller;
    private VelocityTracker velocityTracker;

    /**
     * 菜单保留宽度，用以显示主界面内容
     */
    private int menuHoldWidth;

    /**
     * 一秒移动像素
     */
    private float velocityVal;

    /**
     * 界面偏移时间周期，单位：毫秒
     */
    private static final int ANIMATION_DURATION_TIME = 300;

    private ViewConfiguration viewConf;

    public ScrollContainer(Context context)
    {
        super(context);

        scroller = new Scroller(context);
        menuHoldWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70,
                getResources().getDisplayMetrics());
        viewConf = ViewConfiguration.get(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        velocityTracker = VelocityTracker.obtain();
        velocityTracker.addMovement(ev);

        if (MotionEvent.ACTION_MOVE != ev.getAction()) return false;

        velocityTracker.computeCurrentVelocity(1000, viewConf.getScaledMaximumFlingVelocity());
        velocityVal = Math.abs(velocityTracker.getXVelocity());

        Log.w(ScrollContainer.class.getSimpleName(), "onInterceptTouchEvent ACTION_MOVE velocityVal >> " + velocityVal);

        if (velocityVal > 300) return true;

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        float x = event.getX();

        if (MotionEvent.ACTION_MOVE == event.getAction())
        {
            Log.w(ScrollContainer.class.getSimpleName(), "onTouchEvent ACTION_MOVE x >> " + x);
            this.getChildAt(1).scrollTo(-(int)x, 0);
        } else if (MotionEvent.ACTION_UP == event.getAction())
        {
            float width = this.getWidth();
            float halfWidth = width / 2;

            int childX = this.getChildAt(1).getScrollX();

            if (x < halfWidth)// 向左滑动
            {
                Log.w(ScrollContainer.class.getSimpleName(), "onTouchEvent ACTION_UP x >> " + x + "\thalf >> " + halfWidth + "\tLEFT");

                scroller.startScroll(childX, 0, -childX, 0, ANIMATION_DURATION_TIME);

            } else// 向右滑动
            {
                Log.w(ScrollContainer.class.getSimpleName(), "onTouchEvent ACTION_UP x >> " + x + "\thalf >> " + halfWidth + "\tRIGHT");

                int tox = (int) (width - Math.abs(childX) - menuHoldWidth);
                scroller.startScroll(childX, 0, -tox, 0, ANIMATION_DURATION_TIME);
            }

            this.invalidate();
        }

        return false;
    }

    @Override
    public void computeScroll()
    {
        if (scroller.computeScrollOffset())
        {
            this.getChildAt(1).scrollTo(scroller.getCurrX(), scroller.getCurrY());
            this.postInvalidate();
        }
    }
}


class MainFrame extends FrameLayout
{
    public MainFrame(Context context)
    {
        super(context);

        LayoutInflater lf = LayoutInflater.from(context);
        View view = lf.inflate(R.layout.ui_scroll_frame, null);

        Button but = (Button) view.findViewById(R.id.ui_scroll_frame_but);
        but.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(MainFrame.this.getContext(), "TEST onclick", Toast.LENGTH_SHORT).show();
            }
        });

        this.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }
}

class MenuFrame extends FrameLayout
{
    private String[] arr = new String[]{"First", "Second", "Third"};
    private ArrayAdapter<String> adp;

    public MenuFrame(Context context)
    {
        super(context);

        ListView list = new ListView(context);
        adp = new ArrayAdapter<String>(this.getContext(), android.R.layout.simple_list_item_1, arr);

        list.setAdapter(adp);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Toast.makeText(MenuFrame.this.getContext(), arr[position], Toast.LENGTH_SHORT).show();
            }
        });

        this.addView(list, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }
}